package com.lz.helper.idcard

import android.content.Context
import android.util.Log
import com.invs.UsbSam

//U口身份证识别, 适用于新, 老像素, 辽源通达U口
class UIDCardRequest(context: Context, readFinger: Boolean, handler: IDCardHandler) :
    IDCardRequest(context, readFinger, handler) {

    //身份证识别硬件
    private val usbSam by lazy { openHub(); UsbSam() }

    override fun run() {
        Log.d(TAG, "...... UIDCardRequest thread start ......")
        while (!isQuit()) {
            await()
            if (isQuit()) break
            val res = usbSam.ReadCard(context, readFinger)
            if (res != 0) {
                handler.sendCardError(ERR_INIT)
                usbSam.CloseComm()
            }
            if (isQuit()) break
            if (res == 0 && usbSam.mCard != null) {
                handler.sendCardResult(IDCardInfo(usbSam.mCard))
                delay(DELAYED_SUCCESS)
            } else {
                handler.sendCardError(ERR_OPEN)
                delay(DELAYED_ERR)
            }
        }
        Log.d(TAG, "......... UIDCardRequest quit  .............")
    }

}